package com.erhuogo.service.impl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.erhuogo.bean.GridPageBean;
import com.erhuogo.dao.MessageDao;
import com.erhuogo.entity.Message;
import com.erhuogo.service.MessageService;

/**
 * 留言Service的实现类
 * @author aibinxiao
 * @date 2016年10月26日 上午11:59:51
 */
public class MessageServiceImpl implements MessageService {
	// 日志
	private static Logger logger = LoggerFactory.getLogger(MessageServiceImpl.class);
			
	// 注入MessageDao
	private MessageDao messageDao;

	public MessageDao getMessageDao() {
		return messageDao;
	}

	public void setMessageDao(MessageDao messageDao) {
		this.messageDao = messageDao;
	}

	// 1.分页条件查询留言Service实现类
	@Override
	public GridPageBean<Message> findByPage(String pageNo, String pageSize, String id, String commodityId, String writerId,
			String readerId, String startDate, String endDate) {
		Integer pNo = 1;
		Integer pSize = 10;
		if(!StringUtils.isEmpty(pageNo) && !StringUtils.isEmpty(pageSize)){
			pNo = Integer.valueOf(pageNo);
			pSize = Integer.valueOf(pageSize);
		}
		int records = messageDao.countByParams(id, commodityId, writerId, readerId, startDate, endDate);
		GridPageBean<Message> result = new GridPageBean<Message>(pNo, records, pSize);
		//查询分页后的具体记录
		if(records <= result.getPageSize()){
			List<Message> rows = messageDao.findPageByParams(id, commodityId, writerId, readerId, startDate, endDate);
			result.setRows(rows);
		} else{
			List<Message> rows = messageDao.findPageByParams(id, commodityId, writerId, readerId, startDate, endDate);
			result.setRows(rows);
		}
		result.setPage(pNo);
		result.setRecords(records);
		result.setTotal(result.getTotal());
		return result;
	}

	// 2.根据ID删除留言Service实现类
	@Override
	public void deleteById(String id) throws Exception {
		if(StringUtils.isEmpty(id)){
			throw new Exception("参数ID不能为空");
		}
		messageDao.deleteById(id);
	}
	
}
